<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/24
 * Time: 18:49
 */

namespace App\Game\Traits;

use Crypt_RSA;
use Carbon\Carbon;

trait BbinTrait
{
    /**
     * gameapi
     * 2018/9/24 13:55
     * Administrator
     * getSign
     * 生成签名
     * @param $plaintext
     * @return string
     */
    public function getSign($plaintext)
    {
        $rsa = new Crypt_RSA();
        $rsa->loadKey($this->privateKey);//xxx 私钥匙
        $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
        $rsa->setHash("md5");
        $signature = $rsa->sign($plaintext);
        return base64_encode($signature);
    }

    /**
     * gameapi
     * 2018/9/24 13:55
     * Administrator
     * agRequest
     * 封装ag得请求
     * @param $action
     * @return string
     */
    public function byRequest($action)
    {
        $timestamp = Carbon::now($this->timezone)->timestamp;
        $signature = $this->getSign($this->channelId . $this->thirdParty . $this->tag . $timestamp);
        $json = [
            'json' => [
                'channelId' => $this->channelId,
                'thirdParty' => $this->thirdParty,
                'tag' => $this->tag,
                'action' => $action,
                'live' => 1,
                'timestamp' => $timestamp,
                'signature' => $signature
            ]
        ];
        $result = $this->request('POST', $this->gameUrl, [], $this->header, $json);
        return $result;
    }

    /**
     * gameapi
     * 2018/9/24 15:28
     * Administrator
     * prepareTransfer
     * 预备转账
     * @param $accountUserName
     * @param $type
     * @param $actype
     * @param $price
     * @param $transferid
     * @return array
     */
    public function prepareTransfer($accountUserName, $type, $actype, $price, $transferid)
    {
        $action = [
            'command' => 'preparetransfercredit',
            'parameters' => [
                'loginname' => $accountUserName,
                'billno' => $this->cagent . $transferid,
                'actype' => $actype,
                'type' => $type,
                'credit' => $price,
                'cur' => 'CNY'
            ]
        ];

        $output = $this->agRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                $result = $this->xmlToArray($output['result']);
                if ($result['@attributes']['info'] == 0) {
                    return ['success' => true, 'data' => [], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => [], 'message' => '预备转账查询成功，转账失败原因：' . $result['@attributes']['msg']];
                }
            } else {
                return ['success' => false, 'data' => [], 'message' => '预备转账失败，错误原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 15:28
     * Administrator
     * transferConfirm
     * 确认转账
     * @param $accountUserName
     * @param $type
     * @param $actype
     * @param $price
     * @param $transferid
     * @return array
     */
    public function transferConfirm($accountUserName, $type, $actype, $price, $transferid)
    {
        $action = [
            'command' => 'transfercreditconfirm',
            'parameters' => [
                'loginname' => $accountUserName,
                'billno' => $this->cagent . $transferid,
                'actype' => $actype,
                'type' => $type,
                'credit' => $price,
                'cur' => 'CNY',
                'flag' => 1
            ]
        ];

        $output = $this->agRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                $result = $this->xmlToArray($output['result']);
                if ($result['@attributes']['info'] == 0) {
                    return ['success' => true, 'data' => [], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => [], 'message' => '确认转账查询成功，转账失败原因：' . $result['@attributes']['msg']];
                }
            } else {
                return ['success' => false, 'data' => [], 'message' => '确认转账失败，错误原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 17:27
     * Administrator
     * changeUpdateFiled
     * 变更修改字段
     * @param array $apiData
     */
    public function changeUpdateFiled(array $apiData)
    {
        if (array_key_exists('dataType', $apiData)) {
            if ($apiData['dataType'] === "HSR") {
                $this->updateFiled = config('game.' . $this->platName)[$apiData['dataType']];
            }
        }
    }

    /**
     * gameapi
     * 2018/9/24 17:35
     * Administrator
     * apiDataParse
     * 字段解析
     * @param array $apiData
     * @param array $data
     * @return array
     */
    public function apiDataParse(array $apiData, array $data)
    {
        $data = $this->statusParse($apiData, $data);
        $data = $this->platTypeParse($apiData, $data);
        return $data;
    }

    /**
     * gameapi
     * 2018/9/24 17:36
     * Administrator
     * statusParse
     * 解析状态字段
     * @param array $apiData
     * @param array $data
     * @return array
     */
    private function statusParse(array $apiData, array $data)
    {
        if (array_key_exists('platformType', $apiData)) {
            if ($apiData['platformType'] == 'sport') {
                if (array_key_exists('Result', $apiData)) {
                    switch ($apiData['Result']) {
                        case 'N':
                            $data['status'] = -1;
                            break;
                        case 'C':
                            $data['status'] = -1;
                            break;
                        case 'W':
                            $data['status'] = 1;
                            break;
                        case 'L':
                            $data['status'] = 1;
                            break;
                        case 'LW':
                            $data['status'] = 1;
                            break;
                        case 'LL':
                            $data['status'] = 1;
                            break;
                        case '0':
                            $data['status'] = 1;
                            break;
                        case 'D':
                            $data['status'] = 0;
                            break;
                        case 'F':
                            $data['status'] = -1;
                            break;
                        case 'X':
                            $data['status'] = 0;
                            break;
                        case 'S':
                            $data['status'] = 0;
                            break;
                        case 'SC':
                            $data['status'] = -1;
                            break;
                        case 'SDC':
                            $data['status'] = -1;
                            break;
                        default:
                            $data['status'] = 0;
                            break;
                    }
                }
            } elseif ($apiData['platformType'] == 'casinolive') {
                if (array_key_exists('ResultType', $apiData)) {
                    switch ($apiData['ResultType']) {
                        case ' ':
                            $data['status'] = 1;
                            break;
                        case null:
                            $data['status'] = 1;
                            break;
                        case '-1':
                            $data['status'] = -1;
                            break;
                        case '0':
                            $data['status'] = 0;
                            break;
                        case '1':
                            $data['status'] = 1;
                            break;
                        default:
                            $data['status'] = 0;
                            break;
                    }
                }
            } elseif ($apiData['platformType'] == 'slot') {
                if (array_key_exists('Result', $apiData)) {
                    switch ($apiData['Result']) {
                        case '-1':
                            $data['status'] = -1;
                            break;
                        case '0':
                            $data['status'] = 0;
                            break;
                        case '1':
                            $data['status'] = 1;
                            break;
                        case '200':
                            $data['status'] = 1;
                            break;
                        case '-77':
                            $data['status'] = 0;
                            break;
                        default:
                            $data['status'] = 0;
                            break;
                    }
                }
            } elseif ($apiData['platformType'] == 'lottery') {
                if (array_key_exists('Result', $apiData)) {
                    switch ($apiData['Result']) {
                        case 'W':
                            $data['status'] = 1;
                            break;
                        case 'N':
                            $data['status'] = 1;
                            break;
                        case 'L':
                            $data['status'] = 1;
                            break;
                        case 'N2':
                            $data['status'] = -1;
                            break;
                        case '0':
                            $data['status'] = 0;
                            break;
                        default:
                            $data['status'] = 0;
                            break;
                    }
                }
            } elseif ($apiData['platformType'] == 'fish') {
                if (array_key_exists('Result', $apiData)) {
                    switch ($apiData['Result']) {
                        case 'C':
                            $data['status'] = -1;
                            break;
                        case 'W':
                            $data['status'] = 1;
                            break;
                        case 'L':
                            $data['status'] = 1;
                            break;
                        default:
                            $data['status'] = 0;
                            break;
                    }
                }
            } else {
                if (array_key_exists('Result', $apiData)) {
                    switch ($apiData['Result']) {
                        case '-1':
                            $data['status'] = -1;
                            break;
                        case '0':
                            $data['status'] = 0;
                            break;
                        case '1':
                            $data['status'] = 1;
                            break;
                        default:
                            $data['status'] = 0;
                            break;
                    }
                }
            }

        }

        return $data;
    }

    /**
     * gameapi
     * 2018/9/27 23:11
     * Administrator
     * platTypeParse
     * 解析平台类型
     * @param array $apiData
     * @param array $data
     * @return array
     */
    private function platTypeParse(array $apiData, array $data)
    {
        if (array_key_exists('platformType', $apiData)) {
            switch ($apiData['platformType']) {
                case 'casinolive':
                    $data['plat_type'] = '视讯';
                    break;
                case 'sport':
                    $data['plat_type'] = '体育';
                    break;
                case 'lottery':
                    $data['plat_type'] = '彩票';
                    break;
                case 'slot':
                    $data['plat_type'] = '电子';
                    break;
                case 'fish':
                    $data['plat_type'] = '捕鱼';
                    break;
            }
        }
        return $data;
    }

    private function getBBInGameType($gameType)
    {
        return array_search($gameType, $this->gameType);
    }

}